package io.renren.modules.tbposition.service.impl;

//import io.renren.modules.tbposition.service.PosWebSocketService;
import com.alibaba.fastjson.JSONObject;
import io.renren.modules.tbposition.service.PosWebSocketService;
import org.java_websocket.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.tbposition.dao.TbPositionDao;
import io.renren.modules.tbposition.entity.TbPositionEntity;
import io.renren.modules.tbposition.service.TbPositionService;

import static cn.hutool.core.lang.Console.log;


@Service("tbPositionService")
public class TbPositionServiceImpl extends ServiceImpl<TbPositionDao, TbPositionEntity> implements TbPositionService {

    @Autowired
    PosWebSocketService posWebSocketService;
    @Autowired
    TbPositionDao tbPositionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws IOException {
        IPage<TbPositionEntity> page = this.page(
                new Query<TbPositionEntity>().getPage(params),
                new QueryWrapper<TbPositionEntity>()
        );
//        log.info(new PageUtils(page).toString());
        System.out.println(new PageUtils(page));
//        posWebSocketService.sendMessage(JSONObject.toJSONString(TbPositionEntity));

        return new PageUtils(page);
    }

    @Override
    @Async("screenTaskExecutor")
//    @Scheduled(cron = "0/5 * * * * ?")
    public List<TbPositionEntity> getTbPos() {
        posWebSocketService.sendMessage(JSONObject.toJSONString(tbPositionDao.getTbPos()));
        return tbPositionDao.getTbPos();
    }

    @Override
    public int insertItem(TbPositionEntity tbPositionEntity) {
        posWebSocketService.sendMessage(JSONObject.toJSONString(tbPositionDao.getTbPos()));
        return tbPositionDao.insertItem(tbPositionEntity);
    }


}